package come.itheima.springbootwebmybatistlias.controller;

import come.itheima.springbootwebmybatistlias.anno.Log;
import come.itheima.springbootwebmybatistlias.pojo.Emp;
import come.itheima.springbootwebmybatistlias.pojo.PageBean;
import come.itheima.springbootwebmybatistlias.pojo.Result;
import come.itheima.springbootwebmybatistlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * 员工管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    // 根据分页插件PageHelper分页查询 + 动态SQL（按条件查询）
    @GetMapping
    public Result queryEmpByLimit(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  String name,
                                  Short gender,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {

        log.info("分页查询员工数据：{},{},{},{},{},{}", page, pageSize, name, gender, begin, end);

        PageBean pageBean = empService.queryEmp(page, pageSize, name, gender, begin, end);
        return Result.success(pageBean);
    }


    //批量删除员工
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除,ids{}",ids);
        empService.delete(ids);
        return Result.success();
    }


    //新增员工
    @Log
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工:{}",emp);
        empService.save(emp);
        return Result.success();
    }


    // 根据ID查询员工数据
    @GetMapping("/{id}")
    public Result queryEmpById(@PathVariable Integer id) {
        log.info("根据ID查询员工数据：{}", id);

        Emp emp = empService.queryEmpById(id);
        return Result.success(emp);
    }



    // 修改员工数据（根据上面获得的ID修改）
    @Log
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp) {
        log.info("修改员工数据：{}", emp);
        empService.updateEmp(emp);
        return Result.success();
    }




}
